﻿using System;
using System.Web.UI;
using mojoPortal.Business;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.AppCode.Lib;

namespace mojoPortal.Web.RS.Secure
{
    public partial class RSAfterRegistrationHandler : mojoBasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.SuppressPageMenu();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.IsAuthenticated)
                {
                    SiteUser siteUser = SiteUtils.GetCurrentSiteUser();
                    string registrationPIN = siteUser.GetCustomPropertyAsString("RegistrationPIN");
                    int rsEmployeeId = Convert.ToInt32(siteUser.GetCustomPropertyAsString("EmployeeID"));
                    int mpUserId = siteUser.UserId;
                    string loginName = siteUser.LoginName;
                    string passWord = siteUser.Password;
                    string email = siteUser.Email;
                    string firstName = siteUser.FirstName;
                    string lastName = siteUser.LastName;
                    string currentUser = firstName + lastName;

                    bool isLockedOut = true;
                    if (UpdateReadyServiceUserRegistration(registrationPIN, rsEmployeeId, mpUserId, loginName, passWord, email, currentUser) > 0)
                        isLockedOut = false;

                    if (!isLockedOut)
                        lblRegistrationResultMessage.Text = "Registration Completed Successfully.  Thank You!";
                    else
                        lblRegistrationResultMessage.Text = "Your Registrastion Is Under Review For Approval.  Thank You!";
                }
            }
        }
        private int UpdateReadyServiceUserRegistration(string PIN, int rsEmployeeId, int mpUserId, string username, string password, string email, string currentUser)
        {
            SqlCommand sqlCmd = new SqlCommand();
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@PIN", SqlDbType.NVarChar, 15, ParameterDirection.Input, PIN);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@rsEmployeeId", SqlDbType.Int, 0, ParameterDirection.Input, rsEmployeeId);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@mpUserId", SqlDbType.Int, 0, ParameterDirection.Input, mpUserId);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@UserName", SqlDbType.NVarChar, 50, ParameterDirection.Input, username);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Password", SqlDbType.NVarChar, 25, ParameterDirection.Input, password);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Email", SqlDbType.NVarChar, 50, ParameterDirection.Input, email);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, currentUser);

            SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspUpdateUserRegistration");
            SQLHelper.ExecuteScalarCmd_ReadyServiceMain(sqlCmd);
            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            return returnValue;
        }
        
    }
}